
import 'package:flutter/material.dart';

class NetworkImageWidget extends StatelessWidget{
  ///图片地址
  final String url;
  ///宽度
  final double? width;
  ///高度
  final double? height;
  ///填充方式
  final BoxFit? fit;
  ///是否是圆形
  final bool isCircular;
  ///圆角大小
  final BorderRadiusGeometry radius;
  const NetworkImageWidget({this.url="", this.fit,this.width,this.height,this.isCircular=false,this.radius=BorderRadius.zero});


  Widget  createImage (){
    return  url.isEmpty ?Container(color: Colors.white38)
        :Image.network( url,fit: fit??BoxFit.fill,errorBuilder: (context,url,error)=>Container(color: Colors.white38),
    );
  }

  @override
  Widget build(BuildContext context) {
    var boxRadius=radius;
    if(isCircular){
      boxRadius=BorderRadius.circular(height!);
    }

    return url.isEmpty
        ?Container(decoration: BoxDecoration(color: Colors.white38,borderRadius: boxRadius),width: width,height: height):(
       url.startsWith("http")?Image.network(url,width: width,height: height,
         errorBuilder: (context,url,error)=>Container(decoration: BoxDecoration(color: Colors.white38,borderRadius: boxRadius)),
       ):Image.asset(url,width: width,height: height,gaplessPlayback: true)
    );
  }
}


///圆形图片
class CircularImageWidget extends NetworkImageWidget{
  const CircularImageWidget({ String url="",required double width,required double height})
      :super(url: url,fit: BoxFit.cover,width: width,height: height,isCircular: true);


}

///圆解图片
class RadiusImageWidget extends NetworkImageWidget{

  RadiusImageWidget({String url="",BoxFit? fit,  double? width, double? height,required double radius,BorderRadiusGeometry? boxRadius})
      :super(url: url,fit: fit,width: width,height: height,radius:boxRadius ?? BorderRadius.circular(radius));
}